import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { SemesterService } from './semester.service';
import { CreateSemesterDto } from './dto/create-semester.dto';
import { UpdateSemesterDto } from './dto/update-semester.dto';
import { QuerySemesterDto } from './dto/query-semester.dto';
import { Semester } from './entities/semester.entity';

@ApiTags('学期管理')
@ApiBearerAuth()
@Controller('semester')
export class SemesterController {
    constructor(private readonly semesterService: SemesterService) {}

    @Post()
    @ApiOperation({ summary: '创建新学期' })
    @ApiResponse({ status: 201, description: '创建成功', type: Semester })
    create(@Body() createSemesterDto: CreateSemesterDto) {
        return this.semesterService.create(createSemesterDto);
    }

    @Get()
    @ApiOperation({ summary: '获取学期列表' })
    @ApiResponse({ 
        status: 200, 
        description: '获取成功',
        schema: {
            properties: {
                list: {
                    type: 'array',
                    items: { $ref: '#/components/schemas/Semester' }
                },
                itemCount: { type: 'number' },
                page: { type: 'number' },
                pageSize: { type: 'number' },
                pageCount: { type: 'number' }
            }
        }
    })
    findAll(@Query() query: QuerySemesterDto) {
        return this.semesterService.findAll(query);
    }

    @Get('current')
    @ApiOperation({ summary: '获取当前学期' })
    @ApiResponse({ status: 200, description: '获取成功', type: Semester })
    getCurrentSemester() {
        return this.semesterService.getCurrentSemester();
    }

    @Get('stats')
    @ApiOperation({ summary: '获取学期统计信息' })
    @ApiResponse({ status: 200, description: '获取成功' })
    getSemesterStats() {
        return this.semesterService.getSemesterStats();
    }

    @Get(':id')
    @ApiOperation({ summary: '获取指定学期' })
    @ApiResponse({ status: 200, description: '获取成功', type: Semester })
    findOne(@Param('id') id: string) {
        return this.semesterService.findOne(+id);
    }

    @Patch(':id')
    @ApiOperation({ summary: '更新学期信息' })
    @ApiResponse({ status: 200, description: '更新成功', type: Semester })
    update(@Param('id') id: string, @Body() updateSemesterDto: UpdateSemesterDto) {
        return this.semesterService.update(+id, updateSemesterDto);
    }

    @Patch(':id/status')
    @ApiOperation({ summary: '更新学期状态' })
    @ApiResponse({ status: 200, description: '更新成功', type: Semester })
    updateStatus(
        @Param('id') id: string,
        @Body('status') status: string
    ) {
        return this.semesterService.updateStatus(+id, status);
    }

    @Delete(':id')
    @ApiOperation({ summary: '删除学期' })
    @ApiResponse({ status: 200, description: '删除成功' })
    remove(@Param('id') id: string) {
        return this.semesterService.remove(+id);
    }
}
